package com.codeczx.mymultidownloaddemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private String url1 = "http://dl.pdim.gs/clients/android/pandalive_3.1.7.3811.apk";
    private String url2 = "https://staticlive.douyucdn.cn/upload/client/douyu_client_1_0v2_5_1_1.apk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            startDownload(url1);
                            startDownload(url2);
                        }else{
                            Toast.makeText(MainActivity.this,"permission denied",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void startDownload(String url){
        Intent intent = new Intent(MainActivity.this, DownloadService.class);
        intent.putExtra(DownloadService.DOWNLOAD_URL, url);
        startService(intent);
    }
}
